package org.teachertools.web.forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.teachertools.web.DateConvertUtils;
import org.teachertools.web.struts.BaseActionForm;

public class NoteForm extends BaseActionForm {
    private static final long serialVersionUID = 487163850538043475L;

    private java.lang.Long id = null;
    private java.lang.Long idClasse = null;
    private java.lang.Long eleve = null;
    private java.lang.Long idCategorie = null;
    private java.lang.Long idMatiere = null;
    private java.lang.Long idCompetence = null;
    private java.lang.String note;
    private java.lang.String commentaire;
    private java.lang.String dateNote;
    private java.lang.String finDateNote;
    private Boolean isUpdate;

    public java.lang.Long getId() {
        return this.id;
    }

    public void setId(java.lang.Long pId) {
        this.id = pId;
    }

    public Long getEleve() {
        return this.eleve;
    }

    public void setEleve(Long pEleve) {
        this.eleve = pEleve;
    }

    /**
     * @return the idClasse
     */
    public Long getIdClasse() {
        return this.idClasse;
    }

    /**
     * @param pIdClasse
     *            the idClasse to set
     */
    public void setIdClasse(Long pIdClasse) {
        this.idClasse = pIdClasse;
    }

    /**
     * @return the idCategorie
     */
    public Long getIdCategorie() {
        return this.idCategorie;
    }

    /**
     * @param pIdCategorie
     *            the idCategorie to set
     */
    public void setIdCategorie(Long pIdCategorie) {
        this.idCategorie = pIdCategorie;
    }

    /**
     * @return the idMatiere
     */
    public Long getIdMatiere() {
        return this.idMatiere;
    }

    /**
     * @param pIdMatiere
     *            the idMatiere to set
     */
    public void setIdMatiere(Long pIdMatiere) {
        this.idMatiere = pIdMatiere;
    }

    public Long getIdCompetence() {
        return this.idCompetence;
    }

    public void setIdCompetence(Long pCompetence) {
        this.idCompetence = pCompetence;
    }

    public java.lang.String getNote() {
        return this.note;
    }

    public void setNote(java.lang.String pNote) {
        this.note = pNote;
    }

    public java.lang.String getCommentaire() {
        return this.commentaire;
    }

    public void setCommentaire(java.lang.String pCommentaire) {
        this.commentaire = pCommentaire;
    }

    public java.lang.String getDateNote() {
        return this.dateNote;
    }

    public void setDateNote(java.lang.String pDateNote) {
        this.dateNote = pDateNote;
    }

    public java.lang.String getFinDateNote() {
        return this.finDateNote;
    }

    public void setFinDateNote(java.lang.String pFinDateNote) {
        this.finDateNote = pFinDateNote;
    }

    public Boolean getIsUpdate() {
        return this.isUpdate;
    }

    public void setIsUpdate(Boolean isUpdate) {
        this.isUpdate = isUpdate;
    }

    @Override
    public void reset() {
        this.id = null;
        this.idClasse = null;
        this.eleve = null;
        this.idCategorie = null;
        this.idMatiere = null;
        this.idCompetence = null;
        this.note = null;
        this.commentaire = null;
        this.dateNote = null;
        this.finDateNote = null;
        this.isUpdate = Boolean.FALSE;
    }

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        if (mapping.getPath().startsWith("/init")) {
            return new ActionErrors();
        }

        String valAction = request.getParameter(mapping.getParameter());
        if (getMessage(request, "button.search").equals(valAction)) {
            return validate_search(request);
        }
        if (getMessage(request, "button.save").equals(valAction)) {
            return validate_save(request);
        }
        if (getMessage(request, "button.massiveadd").equals(valAction)) {
            return validate_massiveadd(request);
        }

        return new ActionErrors();
    }

    /**
     * @return
     */
    private ActionErrors validate_massiveadd(HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (this.idCompetence == null) {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.required", getMessage(request,
                    "field.competence")));
        }
        return errors;
    }

    public ActionErrors validate_search(HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (StringUtils.isEmpty(this.note) == false && this.note.length() > 10) {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.fieldtoolong", getMessage(request,
                    "field.note")));
        }
        if (StringUtils.isEmpty(this.dateNote) == false) {
            if (DateConvertUtils.parseDate(this.dateNote, getMessage(request, "FORMAT_DATE_COURT")) == null) {
                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.wrongDate", getMessage(request,
                        "field.dateNote")));
            }
        }
        return errors;
    }

    public ActionErrors validate_save(HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (this.eleve == null) {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.required", getMessage(request,
                    "field.eleve")));
        }
        if (this.idCompetence == null) {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.required", getMessage(request,
                    "field.competence")));
        }
        if (StringUtils.isEmpty(this.note)) {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.required", getMessage(request,
                    "field.note")));
        }
        if (StringUtils.isEmpty(this.note) == false && this.note.length() > 10) {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.fieldtoolong", getMessage(request,
                    "field.note")));
        }
        if (StringUtils.isEmpty(this.dateNote)) {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.required", getMessage(request,
                    "field.dateNote")));
        }
        if (StringUtils.isEmpty(this.dateNote) == false) {
            if (DateConvertUtils.parseDate(this.dateNote, getMessage(request, "FORMAT_DATE_COURT")) == null) {
                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.wrongDate", getMessage(request,
                        "field.dateNote")));
            }
        }
        return errors;
    }

}
